package com.xl.adminserver.v2.dispatch;

import com.google.common.collect.Maps;
import com.xl.adminserver.beans.RestApiResult;
import com.xl.adminserver.v2.sdkclients.ClientFactory;
import com.xl.adminserver.v2.sdkclients.SdkClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class BikeDispatchAction {

    private Map<String, String> pileNoUrlCache = Maps.newHashMap();
    private Map<String, Object> clientCache = Maps.newHashMap();

    public BikeDispatchAction(){
        pileNoUrlCache.put("1111","http://localhost:8083");
    }


    public SdkClient getDispatchClient(String pileNo) {
        String url = pileNoUrlCache.get(pileNo);
        if (url == null || url.isEmpty()) {
            SdkClient sdkClient = ClientFactory.createClient(url, SdkClient.class);
            clientCache.put(url, sdkClient);
            return sdkClient;
        }
        if (!clientCache.containsKey(url)) {
            SdkClient sdkClient = ClientFactory.createClient(url, SdkClient.class);
            clientCache.put(url, sdkClient);
            return sdkClient;
        }
        return (SdkClient) clientCache.get(url);
    }

    /**
     * 测试post
     * @param params
     * @return
     * @throws IOException
     */
    public RestApiResult postBody(Map<String, Object> params) throws IOException {
       SdkClient sdkClient = this.getDispatchClient("1111");
     return  sdkClient.postBodyTest(params).execute().body();
    }


    @Async("taskExecutor")
    public void test1() {
        System.out.println("111111:" + Thread.currentThread().getName());
    }
}
